package edu.chl.da.ejb.ctrl;

import edu.chl.da.ejb.core.Airplane;
import edu.chl.da.ejb.core.Customer;
import edu.chl.da.ejb.core.Flight;
import edu.chl.da.ejb.core.Hotelreservation;
import edu.chl.da.ejb.core.Reservation;
import edu.chl.da.ejb.core.ReservationPK;
import edu.chl.da.ejb.core.Seat;
import edu.chl.da.ejb.persistency.ICustomerFacade;
import edu.chl.da.ejb.persistency.IFlightFacade;
import edu.chl.da.ejb.persistency.IReservationFacade;
import edu.chl.da.ejb.persistency.ISeatFacade;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Max
 */
@Stateless
public class ManageReservation implements IManageReservation {
    @EJB
    ICustomerFacade icf;

    @EJB
    IReservationFacade irf;

    @EJB
    ISeatFacade isf;

    @EJB
    IFlightFacade iff;
    
    @Override
    public Reservation createReservation(Flight flight, Customer customer, Seat seat) {
        Reservation reservation = new Reservation();
        // ReservationPK is the key for reservation
        ReservationPK rpk = new ReservationPK();
        rpk.setAirplane(flight.getAirplane1().getId());
        rpk.setCustomer(customer.getAccount1().getUsername());
        rpk.setFromairport(flight.getFlightPK().getFromairport());
        rpk.setFromdestination(flight.getFlightPK().getFromdestination());
        rpk.setSeatno(seat.getSeatPK().getSeatno());
        rpk.setSeatrow(seat.getSeatPK().getRow());
        reservation.setCustomer1(customer);
        reservation.setFlight(flight);
        reservation.setReservationPK(rpk);
        reservation.setSeat(seat);
        reservation.setTimecreated(new Date());
        List<Hotelreservation> hotelList = new ArrayList<Hotelreservation>();
        reservation.setHotelreservationList(hotelList);
        irf.create(reservation);
        return reservation;
    }

    @Override
    public Reservation editReservation(Reservation reservation) {
        if(reservation == null){
            throw new NullPointerException("Flight is null");
        }
        irf.edit(reservation);
        return reservation;
    }

    @Override
    public Seat getFirstSeat(Airplane airplane) {

        List<Seat> tmpList = isf.findAllByAirplane(airplane);

        for(Seat se : tmpList) {
            if(se.getReservationList().size() == 0) {
                return se;
            }
        }
        return null;
             
    }

    @Override
    public List<Reservation> getReservations(Customer customer) {

        if(customer == null){
           // throw new NullPointerException("Customer is null.");
            return new ArrayList<Reservation>();
        }
        else{
            List<Reservation> tempList = irf.findAll();
            List<Reservation> custResList = new ArrayList<Reservation>();
            for(Reservation r : tempList){
                if(r.getCustomer1().getAccount1().getUsername().equals(customer.getAccount1().getUsername())){
                    custResList.add(r);
                }
            }
            return custResList;
        }
    }
    
    @Override
    public List<Reservation> getReservations(Flight flight){
        if(flight == null){
            throw new NullPointerException("Flight is null.");
        }
        else{
            return iff.find(flight.getFlightPK()).getReservationList();
        }
    }

    @Override
    public boolean removeReservation(Reservation reservation) {
        try {
            irf.remove(reservation);
        }
        catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<Reservation> getAllReservations() {
        return irf.findAll();
    }

}
